# 【1】、全局开始事务
from django.db import transaction
from django.http import HttpResponse


# 1. setting开启自动提交事务
# "ATOMIC_REQUESTS" : True

@transaction.atomic
def has_transaction():
    ...


@transaction.non_atomic_requests
def no_transaction():
    ...


# 【2】、局部开启事务
# 1. 全局不开启事务
# 事务加上
# @transaction.atomic

# 2. 编程式事务
def have_transaction(request):
    # 手动开启事务
    with transaction.atomic():
        # 创建事务回滚点
        sid = transaction.savepoint()
        # 执行逻辑
        try:
            ...
        except Exception as e:
            print(e)
            # 回滚事务
            transaction.savepoint_rollback(sid)
        else:
            # 没有异常则提交事务
            transaction.savepoint_commit(sid)
    return HttpResponse("ok")
